﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using XZTZ.UMP.Public.Models.Flow;
using XZTZ.UMP.WEB.Data;

namespace XZTZ.UMP.WEB.Pages.Flow.FlowTemplateStep
{
    public class EditModel : PageModel
    {
        private readonly XZTZ.UMP.WEB.Data.ApplicationDbContext _context;

        public EditModel(XZTZ.UMP.WEB.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        [BindProperty]
        public Flow_TemplateStep Flow_TemplateStep { get; set; }

        public async Task<IActionResult> OnGetAsync(Guid? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            Flow_TemplateStep = await _context.Flow_TemplateStep
                .Include(f => f.Base_SignatureStepType)
                .Include(f => f.Flow_StepRule).FirstOrDefaultAsync(m => m.guidFlowStepID == id);

            if (Flow_TemplateStep == null)
            {
                return NotFound();
            }
           ViewData["strStepTypeID"] = new SelectList(_context.Set<Base_SignatureStepType>(), "strStepTypeID", "strStepTypeID");
           ViewData["guidRuleID"] = new SelectList(_context.Set<Flow_StepRule>(), "guidRuleID", "guidRuleID");
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.Attach(Flow_TemplateStep).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!Flow_TemplateStepExists(Flow_TemplateStep.guidFlowStepID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return RedirectToPage("./Index");
        }

        private bool Flow_TemplateStepExists(Guid id)
        {
            return _context.Flow_TemplateStep.Any(e => e.guidFlowStepID == id);
        }
    }
}
